﻿//-----------------------------------------------------------------------------------------------------------
// WBFSSync Project by Omega Frost 
// http://wbfssync.codeplex.com/
//
// WBFSSync is Licensed under the terms of the 
// Microsoft Reciprocal License (Ms-RL)
//-----------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WbfsSync.Core.Operations;
using System.Windows;
using System.Threading;
using WBFSe3;
using WbfsSync.Dialogs;
using System.Reflection;

namespace WbfsSync.Core
{
    //-------------------------------------------------------------------------------------------------------
    //
    //-------------------------------------------------------------------------------------------------------
    public static class Update
    {
        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        static WebOperation CurrentOperation;
        static String ServerName = "Codeplex";
        static String ServerUrl = "http://wbfssync.codeplex.com/";


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public static Version Version { get { return Assembly.GetCallingAssembly().GetName().Version; } }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public static WebOperation BeginCheck(OperationDelegate asyncResponse)
        {
            if (CurrentOperation != null)
                return CurrentOperation;
            
                WebOperation op = new WebOperation("Update", OperationType.Update);

                if (asyncResponse != null)
                    op.StepCompleted += asyncResponse;

                op.Dialog = new DialogCopyFiles();
                op.Dialog.Owner = Application.Current.Windows[0];
                op.Dialog.WindowStartupLocation = WindowStartupLocation.CenterOwner;

                op.StepUpdated += new OperationDelegate(UpdateDialogProgress);
                op.Dialog.CancelButton.Click += new RoutedEventHandler(CancelUpdate);

                CurrentOperation = op;
                Threads.LaunchOperation(op, new ParameterizedThreadStart(DownloadAsync));

            UpdateDialogInfo(CurrentOperation, WbfsError.Ok, 0);
            return CurrentOperation;
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        private static void DownloadAsync(object o)
        {
            WebOperation op = o as WebOperation;

            //-----------------------------------------------------------
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        private static void CancelUpdate(object sender, RoutedEventArgs e)
        {
            if (CurrentOperation != null)
                CurrentOperation.Cancel();
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        private static void UpdateDialogInfo(Operation sender, WbfsError error, int element)
        {
            WebOperation op = sender as WebOperation;

            CurrentOperation.Dialog.BeginSetBasicInfo(
                CurrentOperation.Files.Length > 1 ?
                "Downloading " + CurrentOperation.Files.Length +
                " files" : "Downloading 1 file", ServerName,
                "(" + ServerUrl + ")", "Update", "");
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        private static void UpdateDialogProgress(Operation sender, WbfsError error, int element)
        {
            WebOperation op = sender as WebOperation;

            op.TotalCount = op.Files.Length;
            op.Dialog.BeginSetProgress(op.TotalCount, op.IsPaused ? -1 :
                op.Total + ((double)op.Current / (double)op.CurrentCount));
        }
    }
}
